﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Common;
using System.Data.SqlClient;
using Dto;

namespace SqlServerDAL
{
    public class AdminRoleDAL
    {
        private const string CompleteSql = "select * from AdminRole";
        private const string WAMING = "SqlServerDAL.AdminRoleDAL";

        public Hashtable FindById(int id)
        {
            try
            {
                Hashtable ht;
                const string sql = CompleteSql + " where RoleID=@RoleID";
                var param = new SqlParameter("@RoleID", SqlDbType.VarChar) { Value = id };
                using (var red = SqlHelper.ExecuteReader(SqlHelper.connStr, CommandType.Text, sql, param))
                {
                    ht = new Hashtable();
                    while (red.Read())
                    {
                        ht = SqlHelper.FillHashtable(red);
                    }
                }
                return ht;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(WAMING + ".FindById(int id)", ex);
                return null;
            }
        }

        public List<Hashtable> FindAll()
        {
            List<Hashtable> hts;
            using (var red = SqlHelper.ExecuteReader(SqlHelper.connStr, CommandType.Text, CompleteSql))
            {
                hts = new List<Hashtable>();
                while (red.Read())
                {
                    var dto = SqlHelper.FillHashtable(red);
                    hts.Add(dto);
                }
            }
            return hts;
        }

        public bool SaveAdminRole(AdminRoleDto dto)
        {
            try
            {
                string sql;
                var parameters = new List<SqlParameter>();
                if (dto.RoleID == 0)
                {
                    sql = "insert into AdminRole(RoleName,RoleTime,RoleContent) values(@RoleName,@RoleTime,@RoleContent)";
                }
                else
                {
                    sql = "update AdminRole set RoleName=@RoleName,RoleTime=@RoleTime,RoleContent=@RoleContent where RoleID=@RoleID";
                    parameters.Add(new SqlParameter("@RoleID", SqlDbType.Int) { Value = dto.RoleID });
                }
                parameters.Add(new SqlParameter("@RoleName", SqlDbType.VarChar) { Value = dto.RoleName });
                parameters.Add(new SqlParameter("@RoleTime", SqlDbType.DateTime) { Value = dto.RoleTime });
                parameters.Add(new SqlParameter("@RoleContent", SqlDbType.VarChar) { Value = dto.RoleContent });

                SqlHelper.ExecuteNonQuery(SqlHelper.connStr, CommandType.Text, sql, parameters.ToArray());
                return true;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(WAMING + ".SaveAdminRole(AdminRoleDto dto)", ex);
                return false;
            }
        }

        public bool Delete(int id)
        {
            try
            {
                const string sql = "delete from AdminRole where RoleID=@RoleID";
                var param = new SqlParameter("@RoleID", SqlDbType.Int) { Value = id };
                SqlHelper.ExecuteNonQuery(SqlHelper.connStr, CommandType.Text, sql, param);
                return true;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(WAMING + ".Delete(int id)", ex);
                return false;
            }
        }
    }
}
